from django.shortcuts import render
from django.http import HttpResponse
import mysql.connector

def index(request):
    # return HttpResponse('HELLO, world<br/> E:\python3\myblog\myblog1\ blog\ views.py')
    return render(request, 'blog2/index.html',{'hello':'从后台传来'})

def index2(request):
    # return HttpResponse('HELLO, world<br/> E:\python3\myblog\myblog1\ blog\ views.py')
    id = request.GET.get("id")
    return render(request, 'blog2/index2.html',{'hello':'从后台传来到html2'})

def index3(request):
    mydb = mysql.connector.connect(
        host="localhost",  # 数据库主机地址
        user="root",  # 数据库用户名
        passwd="",  # 数据库密码
        database="test"
    )
    mycursor = mydb.cursor()
    mycursor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")
    return HttpResponse('ok')


def index4(request):
    mydb = mysql.connector.connect(
        host="localhost",
        user="root",
        passwd="",
        database="note"
    )
    mycursor = mydb.cursor()
    mycursor.execute("SELECT * FROM note_test")
    myresult = mycursor.fetchall()  # fetchall() 获取所有记录
    mycursor.close()
    mydb.close()

    return render(request, 'blog2/index4.html', {'articles': myresult})

def index5(request):
    mydb = mysql.connector.connect(
        host="47.94.136.51",
        user="root",
        passwd="886400",
        database="test"
    )
    mycursor = mydb.cursor()
    mycursor.execute("SELECT * FROM test_user")
    myresult = mycursor.fetchall()  # fetchall() 获取所有记录
    mycursor.close()
    mydb.close()

    return render(request, 'blog2/index5.html', {'articles': myresult})